package com.itcast.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.reggie.commen.CustomException;
import com.itcast.reggie.mapper.SetmealDao;
import com.itcast.reggie.pojo.Setmeal;
import com.itcast.reggie.pojo.SetmealDish;
import com.itcast.reggie.Dto.SetmealDto;
import com.itcast.reggie.service.SetmealDishService;
import com.itcast.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService{
    @Autowired
    private SetmealDishService setmealDishService;

    //套餐修改页面数据回写
    @Override
    public SetmealDto ReWrite(Long setmealId) {
        //先把Setmeal 的数据写入SetmealDto
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(setmealId);
        BeanUtils.copyProperties(setmeal,setmealDto);
        //再将SetmealDish 的数据写入
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealId);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        //返回 setmealDto
        return setmealDto;
    }

    //修改套餐-第二阶段-修改
    @Override
    public void UpdateById(SetmealDto setmealDto) {
        //保存数据到setmeal表
        this.updateById(setmealDto);
        //保存数据到setmealDish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //清理旧数据
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealDto.getId());
        setmealDishService.remove(wrapper);
        //写入新数据
        for (SetmealDish setmealDish : setmealDishes) {
            //写入setmeal的id值到setmeal的setmeal_id列中
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    //添加新套餐
    @Override
    public void AddSetmeal(SetmealDto setmealDto) {
        //保存数据到setmeal表
         this.save(setmealDto);
        //保存数据到setmealDish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            //写入数据
        for (SetmealDish setmealDish : setmealDishes) {
            //写入setmeal的id值到setmeal的setmeal_id列中
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    //批量起售/停售
    @Override
    public void updateStatus(int status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            setmeal.setStatus(status);
            QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
            wrapper.eq("id",id);
            this.update(setmeal,wrapper);
        }
    }

    //批量删除
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        wrapper.eq("status",1 );
        int count = count(wrapper);
        if(count > 0){
            throw new CustomException("无法删除,至少有一个套餐处于在售状态");
        }
        removeByIds(ids);
        //删除套餐关联的菜品
        QueryWrapper<SetmealDish> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("setmeal_id",ids);
        setmealDishService.remove(wrapper2);
    }
}
